/**************************************************************
 *@ProjectName: blweibo
 *@FileName: MyInfoActivity.java
 *@Date: 2011-5-31
 *@Copyright: 2011 www.hrbanlv.com Inc. All rights reserved.
 **************************************************************/
package com.zhiweibo;

import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.os.AsyncTask.Status;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.zhiweibo.models.StaticInfo;
import com.zhiweibo.models.User;
import com.zhiweibo.util.Constant;
import com.zhiweibo.util.Helper;
import com.zhiweibo.util.HttpUtil;

/**
 * The Class MyInfoActivity.
 * 
 * @author SunHaibin
 * @since Android 1.6
 * @history 2011-5-31
 */
public class MyInfoActivity extends HomeListBaseActivity implements OnClickListener {

    /** The m user. */
    private User mUser;

    /** The iv v. */
    private ImageView ivV;

    /** The iv hr. */
    private ImageView ivHR;

    /** The iv li. */
    private ImageView ivLi;

    /** The iv qi. */
    private ImageView ivQi;

    /** The task. */
    private AsyncTask task;

    /** The wd. */
    private WeiboDB wd;

    /*
     * (non-Javadoc)
     * 
     * @see com.yingnet.zhiweibo.BaseActivity#onClick(android.view.View)
     */
    @Override
    public void onClick(View paramView) {
        if (paramView.getId() == R.id.llAttention) {
            startActivityForResult(
                    new Intent(this, UserWeiboAttentionFansList.class).putExtra("mode", 2).putExtra("uid",
                            mUser.getUid()), 0);
        } else if (paramView.getId() == R.id.rlWeibo) {
            startActivityForResult(new Intent(this, UserWeiboAttentionFansList.class).putExtra("mode", 1), 0);
        } else if (paramView.getId() == R.id.llFans) {
            startActivityForResult(
                    new Intent(this, UserWeiboAttentionFansList.class).putExtra("mode", 3).putExtra("uid",
                            mUser.getUid()), 0);
        } else if (paramView.getId() == R.id.rlFav) {
            startActivityForResult(new Intent(this, FavoriteActivity.class), 0);
        }
        super.onClick(paramView);
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.yingnet.zhiweibo.BaseActivity#onCreate(android.os.Bundle)
     */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setView(R.layout.myinfo);
        setTitleBar(1, getString(R.string.main_edit), getString(R.string.menu_contact), getString(R.string.main_reload));
        ((RelativeLayout) findViewById(R.id.llAttention)).setOnClickListener(this);
        ((LinearLayout) findViewById(R.id.rlWeibo)).setOnClickListener(this);
        ((LinearLayout) findViewById(R.id.llFans)).setOnClickListener(this);
        ((LinearLayout) findViewById(R.id.rlFav)).setOnClickListener(this);
        ivV = (ImageView) findViewById(R.id.ivItemV);
        ivHR = (ImageView) findViewById(R.id.ivItemHR);
        ivLi = (ImageView) findViewById(R.id.ivItemLi);
        ivQi = (ImageView) findViewById(R.id.ivItemQi);
        wd = WeiboDB.getInstance(this);
    }

    /*
     * (non-Javadoc)
     * 
     * @see android.app.Activity#onResume()
     */
    @Override
    protected void onResume() {
        super.onResume();
        if (TextUtils.isEmpty(StaticInfo.user.getUid())) {
            if (task != null && task.getStatus() == Status.RUNNING) {
                task.cancel(true);
            }
            task = new DBTask().execute(0);
        } else {
            mUser = StaticInfo.user;
            setUI();
        }
    }

    /**
     * Sets the ui.
     */
    private void setUI() {
        if (mUser == null) {
            mUser = new User();
        }
        User user = mUser;
        if (user.getV()) {
            ivV.setVisibility(View.VISIBLE);
        }
        if (user.getHR()) {
            ivHR.setVisibility(View.VISIBLE);
        }
        if (user.getL()) {
            ivLi.setVisibility(View.VISIBLE);
        }
        if (user.getQ()) {
            ivQi.setVisibility(View.VISIBLE);
        }
        Bitmap bmFace = HttpUtil.getImage(MyInfoActivity.this, mUser.getFace());
        ((ImageView) findViewById(R.id.ivPortrait)).setImageBitmap(bmFace);
        ((TextView) findViewById(R.id.tvNick)).setText(mUser.getUsername());
        ((TextView) findViewById(R.id.tvAddress_content)).setText(mUser.getProvince());
        ((TextView) findViewById(R.id.tvAccount_info_content)).setText(Helper.getString(this, "username", ""));
        ((TextView) findViewById(R.id.tvAttention_count)).setText(mUser.getFollow_count());
        ((TextView) findViewById(R.id.tvWeibo_count)).setText(mUser.getTopic_count());
        ((TextView) findViewById(R.id.tvFans_count)).setText(mUser.getFans_count());
        ((TextView) findViewById(R.id.tvFav_Count)).setText(mUser.getFavourite_count());
        ((ImageView) findViewById(R.id.ivGental)).setImageResource(mUser.getGender().equals("1") ? R.drawable.icon_male
                : R.drawable.icon_female);
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.yingnet.zhiweibo.HomeListBaseActivity#handleTitleBarEvent(int)
     */
    @Override
    protected void handleTitleBarEvent(int paramInt) {
        switch (paramInt) {
        case 0:
            if (task != null && task.getStatus() == Status.RUNNING) {
                task.cancel(true);
            }
            task = new InfoTask().execute(0);
            break;
        case 1:
            startActivity(new Intent(MyInfoActivity.this, EditActivity.class));
            break;
        }
    }

    /**
     * The Class DBTask.
     * 
     * @author SunHaibin
     * @since Android 1.6
     * @history 2011-5-31
     */
    class DBTask extends AsyncTask {

        /** The pd. */
        private ProgressDialog pd;

        /*
         * (non-Javadoc)
         * 
         * @see android.os.AsyncTask#doInBackground(Params[])
         */
        @Override
        protected User doInBackground(Object... paramArrayOfString) {
            if (!TextUtils.isEmpty(StaticInfo.user.getUid())) {
                mUser = StaticInfo.user;
            } else {
                mUser = wd.selectUser(StaticInfo.mUid);
                if (TextUtils.isEmpty(mUser.getUid())) {
                    mUser = HttpUtil.getUserInfo(MyInfoActivity.this, Constant.USERS_SHOW);
                    if (!TextUtils.isEmpty(mUser.getUid())) {
                        wd.insertUser(mUser);
                    }
                }
            }
            return mUser;
        }

        /*
         * (non-Javadoc)
         * 
         * @see android.os.AsyncTask#onPostExecute(java.lang.Object)
         */
        @Override
        protected void onPostExecute(Object result) {
            if (!TextUtils.isEmpty(mUser.getUid())) {
                StaticInfo.user = mUser;
            }
            setUI();
            if (pd != null) {
                pd.dismiss();
            }
        }

        /*
         * (non-Javadoc)
         * 
         * @see android.os.AsyncTask#onPreExecute()
         */
        @Override
        protected void onPreExecute() {
            pd = new ProgressDialog(MyInfoActivity.this);
            pd.setMessage("请稍候……");
            pd.show();
        }

    }

    /**
     * The Class InfoTask.
     * 
     * @author SunHaibin
     * @since Android 1.6
     * @history 2011-5-31
     */
    class InfoTask extends AsyncTask {

        /** The pd. */
        private ProgressDialog pd;

        /*
         * (non-Javadoc)
         * 
         * @see android.os.AsyncTask#doInBackground(Params[])
         */
        @Override
        protected User doInBackground(Object... paramArrayOfString) {
            mUser = HttpUtil.getUserInfo(MyInfoActivity.this, Constant.USERS_SHOW);
            return mUser;
        }

        /*
         * (non-Javadoc)
         * 
         * @see android.os.AsyncTask#onPostExecute(java.lang.Object)
         */
        @Override
        protected void onPostExecute(Object result) {
            if (!TextUtils.isEmpty(mUser.getUid())) {
                StaticInfo.user = mUser;
                wd.deleteUser();
                wd.insertUser(mUser);
            }
            setUI();
            if (pd != null) {
                pd.dismiss();
            }
        }

        /*
         * (non-Javadoc)
         * 
         * @see android.os.AsyncTask#onPreExecute()
         */
        @Override
        protected void onPreExecute() {
            pd = new ProgressDialog(MyInfoActivity.this);
            pd.setMessage("请稍候……");
            pd.show();
        }
    }
}
